package org.jeecg.modules.business.third.strategy.sap;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.third.entity.WmsInReceiptMaterialVo;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.dock.entity.dto.request.ProductReceptionRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.UBShipmentsRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.ProductReceptionResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    采购入库(辅材) 入库：WMS——>SAP【接口：A806】" 'R24
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r43SapWarehouse")
@Slf4j
@RequiredArgsConstructor
public class R43SapWarehouse implements SapOperateStrategy {

    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    @Override
    public List operate(Map<String, Object> map) {
        log.info("结构件/夹件UB单收货");
        List<WmsInReceiptMaterialVo> wmsInReceiptMaterialVos = (List<WmsInReceiptMaterialVo>) map.get("records");

        List<ProductReceptionRequestDTO> list = new ArrayList<>();
        wmsInReceiptMaterialVos.forEach(t -> {
            ProductReceptionRequestDTO requestDTO = new ProductReceptionRequestDTO();

            SapParamUtil.validWmsInReceiptMaterial(t);

            WmsInReceiptTaskMaterial one = wmsInReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsInReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));
            SapParamUtil.validInTaskRecord(one);
            UBShipmentsRequestDTO ubShipmentsRequestDTO = JSONObject.parseObject(one.getExtraText(), UBShipmentsRequestDTO.class);
            requestDTO.setUbDumpNumber(ubShipmentsRequestDTO.getUbDumpNumber());
            requestDTO.setUbLineProject(ubShipmentsRequestDTO.getUbLineProject());
            requestDTO.setFreightVoucher(ubShipmentsRequestDTO.getFreightVoucher());
            requestDTO.setFreightVoucherProject(ubShipmentsRequestDTO.getFreightVoucherProject());
            requestDTO.setSaleOrderCode(ubShipmentsRequestDTO.getSaleOrderCode());
            requestDTO.setSaleOrderCodeLine(ubShipmentsRequestDTO.getSaleOrderCodeLine());
            String date = DateUtil.formatDate(ubShipmentsRequestDTO.getDeliveryDate()).replace("-", "");
            requestDTO.setDeliveryDate(date);
            list.add(requestDTO);
        });
        List<ProductReceptionResponseDTO> productReceptionResponseDTOS = sapDockingRepeaterService.constructionReceiving(list);
        return productReceptionResponseDTOS;
    }
}
